﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace AssemblyExplorer
{
    public partial class frmSettings : Form
    {
        private TabPage _descrPluginPage = null;
        private IDescriptionPlugin _descriptionPlugin;

        public frmSettings()
        {
            InitializeComponent();
            chbBindingFlags.Items.AddRange(Enum.GetNames(typeof(BindingFlags)));
            descriptionPage.PluginInterfaceType = typeof(IDescriptionPlugin);
            descriptionPage.PluginSelected += new EventHandler(descriptionPage_PluginSelected);
        }

        public IDescriptionStrategy DescriptionStrategy
        {
            get { return _descriptionPlugin.DescriptionStrategy; }
        }

        public StrategySettings DescriptionStrategySettings
        {
            get { return descriptionPage.ProvidedSettings; }
            set
            {
                descriptionPage.LoadSettings(value);
            }
        }

        void descriptionPage_PluginSelected(object sender, EventArgs e)
        {
            StrategyPage strategyPage = sender as StrategyPage;
            IPlugin plugin = strategyPage.Plugin;
            _descriptionPlugin = plugin as IDescriptionPlugin;
            if (plugin.HasOptionsPage)
            {
                if (_descrPluginPage != null)
                    if (tabControl1.TabPages.Contains(_descrPluginPage))
                        tabControl1.TabPages.Remove(_descrPluginPage);
                _descrPluginPage = new TabPage(plugin.Name);
                _descrPluginPage.Controls.Add(plugin.OptionsPage);
                tabControl1.TabPages.Add(_descrPluginPage);
            }
        }

        public BindingFlags BindingFlags
        {
            set 
            {
                Array a = Enum.GetValues(typeof(BindingFlags));
                for (int i = 0; i < a.Length; i++)
                {
                    BindingFlags bf = (BindingFlags)a.GetValue(i);
                    if ((bf & value) == bf)
                    {
                        int index = chbBindingFlags.Items.IndexOf(bf.ToString());
                        chbBindingFlags.SetItemChecked(index, true);
                    }
                }
            }
            get
            {
                BindingFlags result = 0;
                foreach (object o in chbBindingFlags.CheckedItems)
                {
                    BindingFlags bf = (BindingFlags)Enum.Parse(typeof(BindingFlags), o.ToString());
                    result |= bf;
                }
                return result;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (_descriptionPlugin != null) _descriptionPlugin.ApplyOptions();
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

    }

    public enum StrategyOption { Default, Plugin, UserCode }
}
